import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.*;
import java.awt.event.*;

public class RecentScoresScreen {

	// Create a new JFrame with a title of "Coverage Corner"
	JFrame recFrame = new JFrame("Coverage Corner - Recent Scores Screen");
	RecentScore[] recentScores;
	MatchUp[] matchUPS;
	League league;
	
	public RecentScoresScreen() {
		
	}
	
	public void runRecentScoresScreen(League L, MatchUp[] matchup, RecentScore[] recent) {
		Toolkit tk = Toolkit.getDefaultToolkit();
		int screenWidth = ((int) tk.getScreenSize().getWidth());
		int screenHeight = ((int) tk.getScreenSize().getHeight());

		
		this.league = L;
		this.matchUPS = matchup;
		this.recentScores = recent;

		// Set background color to black
		Container con = recFrame.getContentPane();
		con.setBackground(Color.BLACK);
		// Make sure sizing is alright
		recFrame.validate();
		
		//Create new panel, -> set the background to black
		JPanel recentUpdates = new JPanel();
		recentUpdates.setBackground(Color.blue);
		//Create a new vertical box layout, that way when we add the recent updates, it will go top to bottom
		recentUpdates.setLayout(new BoxLayout(recentUpdates, BoxLayout.Y_AXIS));
		//Set the box to the top-left corner of the screen, then top and then left.
		recentUpdates.setBounds(0,0,((int)screenWidth/2),screenHeight);
		recentUpdates.setAlignmentX(Component.LEFT_ALIGNMENT);
		recentUpdates.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		
		MouseListener recentListener = new MouseAdapter() {
			  public void mouseClicked(MouseEvent event) {
			    recFrame.setVisible(false); // figure out the source
				MainScreen ms = new MainScreen();
			    ms.runMainScreen(league,matchUPS,recentScores); // display the text in the clicked label
			    // message is a JLabel
			  }
			};
		recentUpdates.addMouseListener(recentListener);
		
		String recentData[][] = new String[20][3];
		// This for loop is going to get the recently updated scores and add them to the recent scores Panel
		for (int x = 0; x < recent.length; x++) {
			RecentScore rS = recent[x];
			
			recentData[x][0] = "        +" + rS.GetPlusPoints();
			recentData[x][1] = rS.GetTeamName();
			recentData[x][2] = rS.GetDescription();
			
			/*
			//Create a blank JLabel that contains a newline character to space out the recent scores
			JLabel blank = new JLabel("\n");
			JLabel i = new JLabel("      +" + rS.GetPlusPoints() + "        " + rS.GetTeamName() + "                            " + rS.GetDescription());
			//Change the font sizes of both the recent scores info and the blank label
			Font newI = new Font(i.getFont().getName(),i.getFont().getStyle(),(int)recentUpdates.getWidth()/2/20);
			Font newB = new Font(blank.getFont().getName(),blank.getFont().getStyle(),(int)recentUpdates.getWidth()/2/15);
			i.setFont(newI);
			// Change the text color
			i.setForeground(Color.white);
			blank.setFont(newB);
			// Add the 2 labels to the recentUpdates Panel
			recentUpdates.add(blank);
			recentUpdates.add(i);*/
		}
		String colArr[] = new String[3];
		DefaultTableModel model= new DefaultTableModel(recentData,colArr);
		JTable recentTable = new JTable(model);
		recentTable.setBackground(Color.BLUE);
		recentTable.setForeground(Color.WHITE);
		recentTable.setRowHeight(55);
		recentTable.getColumnModel().getColumn(0).setPreferredWidth(20);
		recentTable.getColumnModel().getColumn(1).setPreferredWidth(80);
		recentTable.getColumnModel().getColumn(2).setPreferredWidth(400);
		recentTable.setShowGrid(false);
		Font newF = new Font("rankTable", Font.BOLD,(int)recentUpdates.getWidth()/40);
		recentTable.setFont(newF);
		recentUpdates.add(recentTable);
		recentTable.addMouseListener(recentListener);
		
		
		
		//Create a new JPanel, -> set the background to be black (would like to have an "off-air" picture here instead somehow)
		JPanel photo = new JPanel();
		photo.setBackground(Color.black);
		//Set the correct bounds.
		photo.setBounds(screenWidth - screenWidth/2, 0, screenWidth/2, screenHeight/2);
		photo.setSize(screenWidth,screenHeight);
		photo.setAlignmentX(Component.RIGHT_ALIGNMENT);
		photo.setAlignmentY(Component.TOP_ALIGNMENT);
		photo.addMouseListener(recentListener);
		
		//Create new panel, -> set the background to green
		JPanel stats = new JPanel();
		stats.setBackground(Color.cyan);
		//This is so the matchups go from left to right
		stats.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		//Sets the correct bounds
		stats.setBounds(screenWidth - screenWidth/2, screenHeight - screenHeight/2, screenWidth/2, screenHeight/2);
		stats.setAlignmentX(Component.RIGHT_ALIGNMENT);
		stats.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		stats.addMouseListener(recentListener);
		
		recFrame.add(recentUpdates);
		recFrame.add(stats);
		recFrame.add(photo);
		
		// Pack it all up
		recFrame.pack();

		// Set the default close operation to eit when you click the X button
		recFrame.setDefaultCloseOperation(recFrame.EXIT_ON_CLOSE);
		// Center the JFrame
		recFrame.setLocationRelativeTo(null);
		
		// Set the window to full screen mode
		recFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
		// Set the visibility
		recFrame.setVisible(true);
	}
}
